﻿'// ========================================================================= '
'// ------------------------------------------------------------------------- '
'//!				\file		CApp.vb
'			        \date		june 2010
'			        \author		TNick
'
'			        \brief		
'
'
'   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'   This file is considered universal knowledge. No restrictions
'   are imposed; use as you see fit.
'   Please read ReadMe.txt and Licence.txt in root folder
'   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'
'   The code here implements general application code such as initialization
'   and termination
'// ------------------------------------------------------------------------- '
'// ========================================================================= '
'//
'//
'//
'//
'//  IMPORTS     ------------------------------------------------------------ '
Imports System.Windows.Forms

'//  IMPORTS     ============================================================ '
'//
'//
'//
'//
'//  DEFINITIONS    --------------------------------------------------------- '

Public Delegate Function Del_DataInVal( _
        ByRef sLine As String, _
        ByRef sTokList() As String) As Boolean


'//  DEFINITIONS    ========================================================= '
'//
'//
'//
'//
'//  DATA    ---------------------------------------------------------------- '


'//  DATA    ================================================================ '
'//
'//
'//
'//
'//  CLASS    --------------------------------------------------------------- '
Public Class CD_DataIn

    '// tells if an empty string (resulting from trimming all spaces)
    '// is an allowable input
    Public bAllowEmptyInput As Boolean = False
    '// tells if empty lines are send to callback function
    Public bSendEmptyLines As Boolean = False
    '// the callback
    Public kbValidator As Del_DataInVal = Nothing
    '// valid separators for line elements
    Public inl_sep() As String
    '// file that is loaded in text box
    Public sCrtFile As String = ""

    '// ------------------------------------------------------------------------- '
    Private Sub OK_Button_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles OK_Button.Click

        '// separators for lines
        Dim sLnDiv(0 To 0) As String
        sLnDiv(0) = vbCrLf
        '// resulted lines
        Dim sLines() As String = Nothing
        '// resulted tokens in lines
        Dim sTok() As String

        '// check if empty input
        If (String.IsNullOrEmpty(Me.TX_DataIn.Text.Trim())) Then
            If (bAllowEmptyInput = False) Then
                Autodesk.AutoCAD.ApplicationServices.Application. _
                    ShowAlertDialog("empty input!")
                Return
            End If
            Me.DialogResult = System.Windows.Forms.DialogResult.OK
            Me.Close()
        End If

        '// split input in lines
        If (bSendEmptyLines) Then
            sLines = Me.TX_DataIn.Text.Split(sLnDiv, StringSplitOptions.None)
        Else
            sLines = Me.TX_DataIn.Text.Split(sLnDiv, StringSplitOptions.RemoveEmptyEntries)
        End If

        If (sLines IsNot Nothing) Then
            If (sLines.Length > 0) Then
                '// iterate in lines
                For i As Integer = 0 To sLines.Length - 1
                    sTok = sLines(i).Split(inl_sep, StringSplitOptions.RemoveEmptyEntries)
                    If (kbValidator IsNot Nothing) Then
                        If (kbValidator(sLines(i), sTok) = False) Then
                            Return
                        End If
                    End If
                Next i
            End If
        End If

        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub
    '// ========================================================================= '

    '// ------------------------------------------------------------------------- '
    Private Sub Cancel_Button_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles Cancel_Button.Click

        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub
    '// ========================================================================= '

    '// ------------------------------------------------------------------------- '
    Private Sub CD_DataIn_Load( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles MyBase.Load


    End Sub
    '// ========================================================================= '

    '// ------------------------------------------------------------------------- '
    Private Sub B_Load_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles B_Load.Click

        Try

            Dim fdlg As OpenFileDialog = New OpenFileDialog()

            fdlg.Title = Me.Text
            fdlg.Filter = CMain._T(TX.EXCFILES) & " (*.xls)|*.xls|" & _
                          CMain._T(TX.TXTFILES) & " (*.txt)|*.txt|" & _
                          CMain._T(TX.ALLFILES) & " (*.*)|*.*"
            fdlg.FilterIndex = 2
            fdlg.RestoreDirectory = False
            fdlg.FileName = sCrtFile
            fdlg.CheckFileExists = True
            If fdlg.ShowDialog() = DialogResult.OK Then
                sCrtFile = fdlg.FileName

                Dim srFileReader As System.IO.StreamReader = Nothing
                Dim sInputLine As String
                srFileReader = System.IO.File.OpenText(sCrtFile)
                sInputLine = srFileReader.ReadLine()
                Do Until sInputLine Is Nothing
                    Me.TX_DataIn.Text = Me.TX_DataIn.Text & sInputLine
                    sInputLine = srFileReader.ReadLine()
                Loop
                srFileReader.Close()
            End If

        Catch ex As Exception
            Autodesk.AutoCAD.ApplicationServices.Application. _
                ShowAlertDialog("Failed to load file!")
        End Try
        GC.Collect()
        GC.WaitForPendingFinalizers()
    End Sub
    '// ========================================================================= '

    '// ------------------------------------------------------------------------- '
    Private Sub B_Save_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles B_Save.Click

        Try
            Dim fdlg As SaveFileDialog = New SaveFileDialog()

            fdlg.Title = Me.Text
            fdlg.Filter = CMain._T(TX.EXCFILES) & " (*.xls)|*.xls|" & _
                          CMain._T(TX.TXTFILES) & " (*.txt)|*.txt|" & _
                          CMain._T(TX.ALLFILES) & " (*.*)|*.*"
            fdlg.FilterIndex = 2
            fdlg.RestoreDirectory = False
            fdlg.FileName = sCrtFile
            If fdlg.ShowDialog() = DialogResult.OK Then
                sCrtFile = fdlg.FileName

                Dim srFileWrite As System.IO.StreamWriter = Nothing
                srFileWrite = System.IO.File.CreateText(sCrtFile)
                srFileWrite.Write(Me.TX_DataIn.Text)
                srFileWrite.Close()

            End If

        Catch ex As Exception
            Autodesk.AutoCAD.ApplicationServices.Application. _
                ShowAlertDialog("Failed to load file!")
        End Try
        GC.Collect()
        GC.WaitForPendingFinalizers()
    End Sub
    '// ========================================================================= '

End Class

'//  CLASS    =============================================================== '
'//
'//
'//
'//
'//  FUNCTIONS    ----------------------------------------------------------- '

'//  FUNCTIONS    =========================================================== '
'//
'//
'//
'//
'// ------------------------------------------------------------------------- '
'// ========================================================================= '
